﻿namespace XGameLibrary.SpriteClasses
{
    using System;
    using System.Collections.Generic;

    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Audio;
    using Microsoft.Xna.Framework.Graphics;

    using XGameLibrary.TileEngine;

    public class AnimatedSprite
    {
        public Dictionary<AnimationKey, Animation> animations;
        private AnimationKey currentAnimation;
        private bool isAnimating;

        private Texture2D texture;
        private Vector2 position;
        private Vector2 velocity;
        private float speed = 2.5f;

        public AnimatedSprite(Texture2D sprite, Dictionary<AnimationKey, Animation> animation)
        {
            this.texture = sprite;
            this.animations = new Dictionary<AnimationKey, Animation>();

            foreach (AnimationKey key in animation.Keys)
            {
                animations.Add(key, (Animation)animation[key].Clone());
            }
        }
        public static SoundEffect WalkingOnGrass { get; set; }

        public static SoundEffect WalkingOnRock { get; set; }

        public static SoundEffect WalkingOnWood { get; set; }

        public static Texture2D StaminaBar { get; set; }

        public AnimationKey CurrentAnimation
        {
            get
            {
                return this.currentAnimation;
            }

            set
            {
                this.currentAnimation = value;
            }
        }

        public bool IsAnimating
        {
            get
            {
                return this.isAnimating;
            }

            set
            {
                this.isAnimating = value;
            }
        }

        public int Width
        {
            get
            {
                return this.animations[this.currentAnimation].FrameWidth;
            }
        }

        public int Height
        {
            get
            {
                return this.animations[this.currentAnimation].FrameHeight;
            }
        }

        public float Speed
        {
            get
            {
                return this.speed;
            }

            set
            {
                this.speed = MathHelper.Clamp(speed, 1.0f, 16.0f);
            }
        }

        public Vector2 Position
        {
            get
            {
                return this.position;
            }

            set
            {
                this.position = value;
            }
        }

        public Vector2 Velocity
        {
            get
            {
                return this.velocity;
            }

            set
            {
                this.velocity = value;
                if (this.velocity != Vector2.Zero)
                {
                    velocity.Normalize();
                }
            }
        }

        public void Update(GameTime gameTime)
        {
            if (this.isAnimating)
            {
                animations[currentAnimation].Update(gameTime);
            }
        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch, Camera camera, float barValue)
        {
            spriteBatch.Draw(
                this.texture,
                this.position - camera.Position,
                this.animations[this.currentAnimation].CurrentFrameRect,
                Color.White);

            spriteBatch.Draw(AnimatedSprite.StaminaBar, new Rectangle(
                (int)(this.position.X - camera.Position.X), (int)(this.position.Y - camera.Position.Y) - 5, AnimatedSprite.StaminaBar.Width, 3), new Rectangle(0, 0, AnimatedSprite.StaminaBar.Width, 3), Color.White);
            spriteBatch.Draw(AnimatedSprite.StaminaBar, new Rectangle(
                (int)(this.position.X - camera.Position.X), (int)(this.position.Y - camera.Position.Y) - 5, (int)(AnimatedSprite.StaminaBar.Width * ((double)barValue / 100)), 3), new Rectangle(0, 3, AnimatedSprite.StaminaBar.Width, 3), Color.DarkGreen);
        }

        public void LockToMap()
        {
            this.position.X = MathHelper.Clamp(this.position.X, 0, TileMap.WidthInPixels - Width);
            this.position.Y = MathHelper.Clamp(this.position.Y, 0, TileMap.HeightInPixels - Height);
        }
    }
}
